********************************************************************************
** Do file: g5_endline_analysis.do
** First started: April 22, 2020 
** Last edited: December 31, 2023

/* Purpose: this do file analyzes several outcome variables from the endline survey.
The preamble develops temporary individual and household level datasets.

Part I: Effect on Asset Purchases, Sales, and Money Borrowed in the Last 6 Months, Levels
Part II: Household Balance Sheet, Levels
Part III: Shocks variables

* ITEM MADE: TABLE 5
* ITEM MADE: TABLE 6
* ITEM MADE: TABLE 8
*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

** Preamble - data management for both the individual level dataset and household level dataset **

** Household **

* Load key data first
use "$merged/key_rep.dta", clear
keep if hhid!=.

* Restrict down to sample of interest
keep if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_surveyed==1 & k_surveyed_end==1

* Merge in variables from both baseline and endline
merge 1:1 hhid using "$bsvy_clean/hhvars_baseline", keepusing(value_hh_assets total_loans)
keep if _merge==3 | _merge==1
drop _merge

* NOTE: Rename the baseline version of total loans
ren total_loans total_loans_bl

* Merge endline survey variables
merge 1:1 hhid using "$esvy_clean/3A_assets_hh", keepusing(buy_assets_val99p sell_assets_val99p)
keep if _merge==3 | _merge==1
drop _merge

merge 1:1 hhid using "$esvy_clean/6_bsl", keepusing(amt_forminform_win net_assets_loans_a)
keep if _merge==3 | _merge==1
drop _merge

merge 1:1 hhid using "$esvy_clean/7_wellbeing_hh", keepusing(shockAindex scaleAindex shockBindex scaleBindex)
keep if _merge==3 | _merge==1
drop _merge
		
* Drop the choice group as well
drop if treatmenttype_sh=="R T3"

* Develop indicators
g anytreat_assigned = (treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U") 
g locked_assigned = (treatmenttype_sh=="R T1-L")
g surprise_assigned = (treatmenttype_sh=="R T1-U")
g unlocked_assigned = (treatmenttype_sh=="R T2-U")

* Change several values here to be USD
replace value_hh_assets = value_hh_assets/3704
replace total_loans_bl = total_loans_bl/3704

* Construct varriables here
g assets_val_end = value_hh_assets + buy_assets_val99p - sell_assets_val99p

*************************************************
*************************************************

* Construct varriables here
g total_loans_end_r = total_loans_bl + amt_forminform_win
g av_tl_end_r = assets_val_end - total_loans_end_r

g assets_sample2_r = 1 if assets_val_end!=. & total_loans_end_r!=. & av_tl_end_r!=. & amt_forminform_win!=. 

* Make the samples the same, they are close
g assets_sample_full = 1 if assets_sample2_r==1



************
** Part I **
************

*** Effect on Asset Purchases, Sales, and Money Borrowed in the Last 6 Months, Regressions, Levels (ITT)

***** TABLE 5 *****

* Outcome means
sum buy_assets_val99p if anytreat_assigned!=. & assets_sample_full==1 & anytreat_assigned==0
sum sell_assets_val99p if anytreat_assigned!=. & assets_sample_full==1 & anytreat_assigned==0
sum amt_forminform_win if anytreat_assigned!=. & assets_sample_full==1 & anytreat_assigned==0
sum net_assets_loans_a if anytreat_assigned!=. & assets_sample_full==1 & anytreat_assigned==0

reg buy_assets_val99p locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned

reg sell_assets_val99p locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned

reg amt_forminform_win locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned

reg net_assets_loans_a locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned


* OLS results
eststo: reg buy_assets_val99p anytreat_assigned if assets_sample_full==1
eststo: reg buy_assets_val99p locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

eststo: reg sell_assets_val99p anytreat_assigned if assets_sample_full==1
eststo: reg sell_assets_val99p locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

eststo: reg amt_forminform_win anytreat_assigned if assets_sample_full==1
eststo: reg amt_forminform_win locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

eststo: reg net_assets_loans_a anytreat_assigned if assets_sample_full==1
eststo: reg net_assets_loans_a locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

esttab using "$tables/assets99_lvl_ITT_v2.tex", ///
	b(0) se(0) se replace booktabs star(* .10 ** .05 *** .01) title(Effect on Asset Purchases, Sales, Money, and Money Borrowed in the Last 6 months\label{assets99_lvl_ITT_v2}) ///
	label nodepvars mtitles("Asset purchases" "Asset purchases" "Asset sales" "Asset sales" "Money borrowed" "Money borrowed" "Net difference" "Net difference") nonotes ///
	varlabels(anytreat_assigned "Pooled" locked_assigned "Secured" surprise_assigned "Surprise Unsecured" unlocked_assigned "Unsecured" _cons "Constant", ///
	elist(anytreat_assigned \addlinespace locked_assigned \addlinespace surprise_assigned \addlinespace unlocked_assigned \addlinespace)) 
eststo clear

* Fact for paper: total borrowing changed by what % of the loan?
reg amt_forminform_win anytreat_assigned if assets_sample_full==1
display 40/81 // ~ 50%



*************
** Part II **
*************


*** Effect on Household Balance Sheet, Regressions, Levels (ITT)

***** TABLE 6 *****

* Outcome means
sum assets_val_end if anytreat_assigned == 0 & assets_sample_full==1
sum total_loans_end_r if anytreat_assigned == 0 & assets_sample_full==1
sum av_tl_end_r if anytreat_assigned == 0 & assets_sample_full==1

* tests
reg assets_val_end locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned

reg total_loans_end_r locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned

reg av_tl_end_r locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1
test locked_assigned = unlocked_assigned

* OLS results, Levels
eststo: reg assets_val_end anytreat_assigned if assets_sample_full==1
eststo: reg assets_val_end locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

eststo: reg total_loans_end_r anytreat_assigned if assets_sample_full==1
eststo: reg total_loans_end_r locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

eststo: reg av_tl_end_r anytreat_assigned if assets_sample_full==1
eststo: reg av_tl_end_r locked_assigned surprise_assigned unlocked_assigned if assets_sample_full==1

esttab using "$tables/assetsbal99_lvl_ITT_v2.tex", ///
	b(0) se(0) se replace booktabs star(* .10 ** .05 *** .01) title(Effect on Household Balance Sheet\label{assetsbal99_lvl_ITT_v2}) ///
	label nodepvars mtitles("Asset value" "Asset value" "Debt" "Debt" "Net difference" "Net difference") nonotes ///
	varlabels(anytreat_assigned "Pooled" locked_assigned "Secured" surprise_assigned "Surprise Unsecured" unlocked_assigned "Unsecured" _cons "Constant", ///
	elist(anytreat_assigned \addlinespace locked_assigned \addlinespace surprise_assigned \addlinespace unlocked_assigned \addlinespace)) 
eststo clear


	
**************
** Part III **
**************

***** TABLE 8 *****

*** Shocks over the past 6 months
* A shocks: Problem with money
sum shockAindex if anytreat_assigned==0
sum scaleAindex if anytreat_assigned==0

eststo: reg shockAindex anytreat_assigned
eststo: reg shockAindex locked_assigned surprise_assigned unlocked_assigned

eststo: reg scaleAindex anytreat_assigned
eststo: reg scaleAindex locked_assigned surprise_assigned unlocked_assigned

esttab using "$tables/shock_A.tex", ///
	b(2) se(2) se replace booktabs star(* .10 ** .05 *** .01) title(Liquidity Shocks over the Past 6 Months\label{shock_A}) ///
	label nodepvars mtitles("Proportion shocks experienced" "Proportion shocks experienced" "Are you worried about coping with this shock?" "Are you worried about coping with this shock?") nonotes ///
	varlabels(anytreat_assigned "Pooled" locked_assigned "Secured" surprise_assigned "Surprise Unsecured" unlocked_assigned "Unsecured" _cons "Constant", ///
	elist(anytreat_assigned \addlinespace locked_assigned \addlinespace surprise_assigned \addlinespace unlocked_assigned \addlinespace)) 
eststo clear

* B shocks: Money matters for coping
sum shockBindex if anytreat_assigned==0
sum scaleBindex if anytreat_assigned==0

eststo: reg shockBindex anytreat_assigned
eststo: reg shockBindex locked_assigned surprise_assigned unlocked_assigned

eststo: reg scaleBindex anytreat_assigned
eststo: reg scaleBindex locked_assigned surprise_assigned unlocked_assigned

esttab using "$tables/shock_B.tex", ///
	b(2) se(2) se replace booktabs star(* .10 ** .05 *** .01) title(Liquidity Shocks over the Past 6 Months\label{shock_B}) ///
	label nodepvars mtitles("Proportion shocks experienced" "Proportion shocks experienced" "Are you worried about coping with this shock?" "Are you worried about coping with this shock?") nonotes ///
	varlabels(anytreat_assigned "Pooled" locked_assigned "Secured" surprise_assigned "Surprise Unsecured" unlocked_assigned "Unsecured" _cons "Constant", ///
	elist(anytreat_assigned \addlinespace locked_assigned \addlinespace surprise_assigned \addlinespace unlocked_assigned \addlinespace)) 
eststo clear
